<!DOCTYPE html>
<html>

<head>
    <title>Draw a triangle</title>
</head>

<body onload="init()">
    <canvas id="myCanvas" width="640" height="480"></canvas>


    <button onclick="render();">RENDER</button>
    <button onclick="test();">test</button>
</body>

<script id="shaderVs" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    void main() {
        gl_Position = a_Position;
    }
</script>

<script id="shaderFs" type="x-shader/x-fragment">
    void main() {
        gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
</script>

<script>
    var n, gl;

    function init() {
        // Get canvas object from the DOM
        var canvas = document.getElementById("myCanvas");

        // Init WebGL context
        gl = canvas.getContext("webgl");
        if (!gl) {
            console.log("Failed to get the rendering context for WebGL");
            return;
        }

        // Init shaders
        var vs = document.getElementById('shaderVs').innerHTML;
        var fs = document.getElementById('shaderFs').innerHTML;
        if (!initShaders(gl, vs, fs)) {
            console.log('Failed to intialize shaders.');
            return;
        }

        // Write the positions of vertices to a vertex shader
        n = initVertexBuffers(gl);
        if (n < 0) {
            console.log('Failed to set the positions of the vertices');
            return;
        }

        // Clear canvas
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.clear(gl.COLOR_BUFFER_BIT);

        // Draw
        gl.drawArrays(gl.TRIANGLES, 0, n);
    }

    function test() {

        var _t0 = Date.now();

        var j = 0;
        for(var i = 0; i < 50000; i++) {
            j = j + i;
        }

        var _t1 = Date.now();
        console.log(` test cost: ${_t1 - _t0}  j: ${j}`);
    }

    function render() {
        // Clear canvas
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.clear(gl.COLOR_BUFFER_BIT);

        var _t0 = Date.now();
        // Draw
        for(var i = 0; i < 50000; i++) {
            gl.drawArrays(gl.TRIANGLES, 0, n);
        }
        var _t1 = Date.now();

        console.log(` render cost: ${_t1 - _t0} `);
    }

    function initVertexBuffers(gl) {
        // Vertices
        var dim = 3;
        var vertices = new Float32Array([
            0, 0.5, 0,  // Vertice #1
            -0.5, -0.5, 0, // Vertice #2
            0.5, -0.5, 0 // Vertice #3
        ]);

        // Create a buffer object
        var vertexBuffer = gl.createBuffer();
        if (!vertexBuffer) {
            console.log('Failed to create the buffer object');
            return -1;
        }
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

        // Assign the vertices in buffer object to a_Position variable
        var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
        if (a_Position < 0) {
            console.log('Failed to get the storage location of a_Position');
            return -1;
        }
        gl.vertexAttribPointer(a_Position, dim, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(a_Position);

        // Return number of vertices
        return vertices.length / dim;
    }

    function initShaders(gl, vs_source, fs_source) {
        // Compile shaders
        var vertexShader = makeShader(gl, vs_source, gl.VERTEX_SHADER);
        var fragmentShader = makeShader(gl, fs_source, gl.FRAGMENT_SHADER);

        // Create program
        var glProgram = gl.createProgram();

        // Attach and link shaders to the program
        gl.attachShader(glProgram, vertexShader);
        gl.attachShader(glProgram, fragmentShader);
        gl.linkProgram(glProgram);
        if (!gl.getProgramParameter(glProgram, gl.LINK_STATUS)) {
            alert("Unable to initialize the shader program");
            return false;
        }

        // Use program
        gl.useProgram(glProgram);
        gl.program = glProgram;

        return true;
    }

    function makeShader(gl, src, type) {
        var shader = gl.createShader(type);
        gl.shaderSource(shader, src);
        gl.compileShader(shader);
        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert("Error compiling shader: " + gl.getShaderInfoLog(shader));
            return;
        }
        return shader;
    }
</script>

</html>